﻿using DocumentMakingTools.Models;
using Newtonsoft.Json;
using System.Text;

namespace DocumentMakingTools;

public partial class App : Application
{
	public static SettingModel AppSetting { get; set; }
	public App()
	{
		InitializeComponent();

        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

        MainPage = new AppShell();
	}
	protected override async void OnStart()
	{
		base.OnStart();

        AppSetting = new SettingModel();

		// 读文件
		try
		{
            using Stream fileStream = new FileStream(Path.Combine(FileSystem.Current.AppDataDirectory, "AppSettings"), FileMode.Open);
            using StreamReader reader = new StreamReader(fileStream);

            string content = await reader.ReadToEndAsync();

            AppSetting = JsonConvert.DeserializeObject<SettingModel>(content);
        }
		catch (FileNotFoundException)
		{
			AppSetting = new SettingModel
			{
				DefaultOpenCodeDocumentMode = true,
				DefaultOutputPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
			};
		}
    }
}
